<?php
/**
 * Framework_Presenter
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @license http://www.opensource.org/licenses/gpl-license.php
 * @package BAMF
 * @filesource
*/

/**
 * Framework_Presenter
 *
 * Presenter factory class. This is used by the controller file, in 
 * conjunction with the Framework_Module::$presenter variable to produced the
 * desired presenter class.
 *
 * @author Application Support Group
 * @package BAMF
 * @see Framework_Module::$presenter, Framework_Presenter_common
*/
class Framework_Presenter_PresenterFactory {

    /**
     * factory
     *
     * @author Application Support Group
     * @access public
     * @param string $type Presentation type (our view)
     * @param mixed $module Our module, which the presenter will display
     * @return mixed PEAR_Error on failure or a valid presenter
     * @static
    */
	public static function build( $type, $module=null ) {

		if( !$module instanceof Framework_Module ) {
			return Framework_ErrorHandler::raiseError( 
				"Property \"$module\" is not a valid Framework_Module object", 
				__METHOD__."::".__LINE__ 
				) ;
		}

		$class = 'Framework_Presenter_'.$type;
		if( @class_exists( $type )) {

			$presenter = new $type($module);
			if ($presenter instanceof Framework_Presenter_Common) {
				return $presenter;
			} else {
				return Framework_ErrorHandler::raiseError('Invalid presentation class: '.$type, __METHOD__."::".__LINE__);
			}

		} elseif ( class_exists( $class )) {

			$presenter = new $class($module);
			if ($presenter instanceof Framework_Presenter_Common) {
				return $presenter;
			} else {
				return Framework_ErrorHandler::raiseError('Invalid presentation class: '.$type, __METHOD__."::".__LINE__);
			}

		} else {
			return Framework_ErrorHandler::raiseError('Presentation class not found: '.$type, __METHOD__."::".__LINE__);
		}

	}

}// end class

?>
